package org.ssp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ssp.dao.BackupDao;
import org.ssp.dao.TravelDao;
import org.ssp.dao.UserDao;
import org.ssp.impl.BackupImpl;
import org.ssp.impl.TravelImpl;
import org.ssp.impl.UserImpl;
import org.ssp.pojo.Backup;
import org.ssp.pojo.Travel;
import org.ssp.pojo.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


/**
 * 旅游相关操作
 * 
 *
 */
@WebServlet("/travel")
public class TravelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method=request.getParameter("method");
		/*if(request.getSession().getAttribute("assist")==null){//判断session是否失效
			request.getRequestDispatcher("/WEB-INF/page/admin/adm-login.jsp").forward(request, response);
			return;
		}*/
		if("page".equals(method)){//1,请求travel list页
			//查数据库取出所有文章的list
			request.getRequestDispatcher("/WEB-INF/page/admin/travel.jsp").forward(request, response);
		}else if("list".equals(method)){//2,装载活动列表
			System.out.println("travel.....list.....");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			TravelDao traveld=new TravelImpl();
			ArrayList<Travel> list=traveld.findAll(offset, rows);
			total=traveld.findAll();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("list10".equals(method)){
			System.out.println("list10");
			int rows=10;
			int offset=0;
			TravelDao td=new TravelImpl();
			ArrayList<Travel> list=td.find10(offset,rows);
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			JSONObject json=new JSONObject();
			json.element("total", 10);
		    json.element("rows", JSONArray.fromObject(list,jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}
		else if("add".equals(method)){//3,发布活动
			System.out.println("travel.....add.....");
		    String url=request.getParameter("firstimgurl");
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			String money=request.getParameter("money");
			System.out.println(url);
			System.out.println(title);
			System.out.println(content);
			System.out.println(money);
			Travel t=new Travel();
			t.setTitle(title);
			t.setContent(content);
			t.setFirstimgurl(url);
			t.setMoney(Double.valueOf(money));
			TravelDao traveldao=new TravelImpl();
			traveldao.addTravel(t);//存入数据库
			JSONObject json=new JSONObject();
			json.element("success",true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("edit".equals(method)){//4,编辑活动
			System.out.println("travel.....edit...");
			String url=request.getParameter("firstimgurl");
			String id=request.getParameter("id");
			String title=request.getParameter("title");
			String content=request.getParameter("content");
			String money=request.getParameter("money");
			String datetime=request.getParameter("datetime");
			System.out.println(id);
			System.out.println(url);
			System.out.println(title);
			System.out.println(content);
			System.out.println(money);
			System.out.println(datetime);
			Travel t=new Travel();
			t.setTitle(title);
			t.setContent(content);
			t.setFirstimgurl(url);
			t.setMoney(Double.valueOf(money));
			t.setDatetime(new Date(datetime));
			t.setId(Integer.valueOf(id));
			TravelDao traveld=new TravelImpl();
			traveld.updateTravel(t);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;			
		}else if("delete".equals(method)){//5,删除活动
			System.out.println("travel....delete...");
		    String id=request.getParameter("id");
		    TravelDao td=new TravelImpl();
		    td.deleteTravel(Integer.parseInt(id));
		    JSONObject json=new JSONObject();
		    json.element("success", true);
		    PrintWriter out=response.getWriter();
		    out.write(json.toString());
		    return;
		}else if("users".equals(method)){//6,参加某活动的人员名单
			System.out.println("travel....users...");
			int travelid=Integer.valueOf(request.getParameter("id"));
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			UserDao userd=new UserImpl();
			ArrayList<User> list=userd.findByTravelId(travelid, offset, rows);
			total=userd.findByTravelId(travelid);
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("backadd".equals(method)){//7,给活动添加备注
			System.out.println("travel....addback....");
			String assistid=request.getParameter("assistid");
			String content=request.getParameter("content");
			String travelid=request.getParameter("travelid");
			System.out.println(assistid);
			System.out.println(content);
			System.out.println(travelid);
			Backup back=new Backup();
			back.setContent(content);
			back.setTravelid(Integer.valueOf(travelid));
			back.setAssistid(assistid);
			BackupDao backd=new BackupImpl();
			backd.addBackup2(back);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}else if("backlist".equals(method)){//8,某活动的备份列表
			System.out.println("member.....backlist....");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			System.out.println(page+";...........;"+rows);
			int offset=(page-1)*rows;
			int total=0;
			String travelid=request.getParameter("travelid");
			ArrayList<Backup> list=new ArrayList<Backup>();
			BackupDao backd=new BackupImpl();
			list=backd.findByTravelid(Integer.valueOf(travelid), offset, rows);
			total=backd.findByTravelid(Integer.valueOf(travelid));
			JSONObject json=new JSONObject();
			json.element("rows", list);
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("backdelete".equals(method)){//8,删除某条备份
			System.out.println("travel....delete...");
			String id=request.getParameter("id");
			System.out.println(id);
			BackupDao backd=new BackupImpl();
			backd.deleteBackup2(Integer.valueOf(id));
			JSONObject json=new JSONObject();
			json.element("success",true);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}else if("backedit".equals(method)){//9,修改备注信息
			System.out.println("travel....backedit....");
			String id=request.getParameter("id");
			String content=request.getParameter("content");
			String datetime=request.getParameter("datetime");
			Backup back=new Backup();
			back.setId(Integer.valueOf(id));
			back.setContent(content);
			back.setDatetime(datetime);
			BackupDao backd=new BackupImpl();
			backd.updateBackup2(back);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("item".equals(method)){
			String id=request.getParameter("id");
			Travel t=new Travel();
			TravelDao traveld=new TravelImpl();
			t=traveld.findById(Integer.valueOf(id));
			System.out.println(t.getContent());
			if(t!=null){
				request.setAttribute("obj", t);
			}
			request.getRequestDispatcher("/WEB-INF/page/admin/dashboard-item.jsp").forward(request, response);
		}else if("travel-phone".equals(method)){//手机端travel列表
			TravelDao tdo=new TravelImpl();
			ArrayList<Travel> list=tdo.findAllMobile();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("travels", JSONArray.fromObject(list,jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("mytravels".equals(method)){//手机端我的活动
			System.out.println("travel...mytravels...");
			String openid=request.getParameter("openid");
			TravelDao td=new TravelImpl();
			ArrayList<Travel> list=td.findByOpenid(openid);
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("travels", JSONArray.fromObject(list,jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("travelinfo".equals(method)){//手机端活动详情
			System.out.println("trvel....travelinfo....");
			int id=Integer.valueOf(request.getParameter("id"));
			
			TravelDao td=new TravelImpl();
			Travel travel=td.findById(id);
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("travel", JSONObject.fromObject(travel,jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}
		else if("mobile".equals(method)){
			System.out.println("....mobile.....travel");
			String page=request.getParameter("page");
			if("travel".equals(page)){//手机端显示travel list
				TravelDao tdo=new TravelImpl();
				ArrayList<Travel> list=tdo.findAllMobile();
				request.setAttribute("travels", list);
				request.getRequestDispatcher("/WEB-INF/page/user/travel.jsp").forward(request, response);
			}else if("read".equals(page)){//查看travel详情
				String id=request.getParameter("id");
				TravelDao td=new TravelImpl();
				Travel t=td.findById(Integer.parseInt(id));
				request.setAttribute("article",t);
				request.getRequestDispatcher("/WEB-INF/page/user/traveldetail.jsp").forward(request, response);
			}else if("join".equals(page)){
				//1.如果该用户还未
				request.getRequestDispatcher("/WEB-INF/page/user/applyform.jsp").forward(request, response);
			}
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
